package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.SysMonitorSiteInfo;
import com.emep.changzhi.analyse.entity.basics.SysPollutantParameter;
import com.emep.changzhi.analyse.repository.basics.PollutantParameterRepository;
import com.emep.changzhi.analyse.repository.basics.SysMonitorSiteInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; /**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/8/23 16:31
 */

@Service
public class PollutantParameterService {
    @Resource
    private PollutantParameterRepository repository;



    private static final Logger logger = LoggerFactory.getLogger(PollutantParameterService.class);



    /**
     * 添加污染物
     * @param payload
     * @return
     */
    public SysPollutantParameter save(Map<String, Object> payload) {

        SysPollutantParameter sysPollutantParameter1 = null;
        try {
            SysPollutantParameter sysPollutantParameter = (SysPollutantParameter) MapUtils.convertMap(SysPollutantParameter.class,payload);
            sysPollutantParameter1=repository.save(sysPollutantParameter);
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return sysPollutantParameter1;
    }

    /**
     * 根据id删除污染物
     * @param id
     * @return
     */
    public Boolean delete(String id) {
        boolean result = false;
        try {
            repository.deleteById(id);
            result =true;
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     * 查询所有污染物
     * @return
     */
    public List<SysPollutantParameter> findAll() {
        List<SysPollutantParameter> sysPollutantParameterList = null;
        try {
            sysPollutantParameterList =  repository.findAll();
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return sysPollutantParameterList;
    }

    /**
     * 根据id查询污染物
     * @param id
     * @return
     */
    public SysPollutantParameter findById(String id) {
        SysPollutantParameter sysPollutantParameter = null;
        try {
            sysPollutantParameter = repository.findSysPollutantParameterById(id);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return sysPollutantParameter;
    }

    /**
     *根据areaCode查询污染物
     * @param areaCode
     * @return
     */
    public List<String> findByAreaCode(String areaCode) {

        List<String> list = new ArrayList<>();
        try {
            List<SysPollutantParameter> list1= repository.findByAreaCode(areaCode);
            if(list1 !=null){
                for (SysPollutantParameter s:list1){
                    list.add(s.getPollutantName());
                }
            }

        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return list;
    }

    /**
     * 修改
     * @param payload
     * @return
     */
    public Boolean update(Map<String, Object> payload) {

        boolean result = false;
        try {
            SysPollutantParameter sysPollutantParameter = (SysPollutantParameter) MapUtils.convertMap(SysPollutantParameter.class,payload);
            repository.save(sysPollutantParameter);
            result =true;
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return result;
    }
}
